package com.mall.web.control.app.result;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONArray;
import com.mall.client.item.TableNumService;
import com.mall.client.mall.MallService;
import com.mall.client.order.MallItemOrderService;
import com.mall.pojo.Mall;
import com.mall.pojo.MallItemOrder;
import com.mall.pojo.User;
import com.mall.vo.item.MallItemVO;
import com.mall.web.annotation.UserSecurity;
import com.mall.web.util.HttpUtil;
import com.mall.web.util.WebUtil;


@Controller
@RequestMapping("/result/")
public class MallResultController {
	
	@Autowired
	private MallService mallService;
	@Autowired
	private MallItemOrderService mallItemOrderService;
	@Autowired
	private TableNumService tableNumService;
	
	public static int TRADE_TYPE = 1;//付款交易
	public static int ZHIHUI_ORDER_TYPE = 2;//智慧餐厅下单成功
	public static int COMMENT_TYPE =3;//评论成功
	public static int TUAN_TRADE_TYPE = 4;//团购交易
	
	public static String UTF8="UTF-8";
	
	/**
	 * 处理团购的业务逻辑
	 * @param request
	 * @param modelMap
	 */
	private void doSuccTuanCodeBiz(HttpServletRequest request,ModelMap modelMap,int type){
		if (type != TUAN_TRADE_TYPE) {
			return ;
		}
		String tuancode =  request.getParameter("tuancode");
		if(StringUtils.isNotBlank(tuancode)){
			String[] codes = tuancode.split(",");
			List<String> codeList = Arrays.asList(codes);
			modelMap.put("codeList", codeList);
		}
	}
	/**
	 * 成功
	 * @param request
	 * @param response
	 * @param modelMap
	 * @return
	 */
	@UserSecurity
	@RequestMapping("success")
	public String success(HttpServletRequest request,HttpServletResponse response,ModelMap modelMap){
		//start
		Integer type = HttpUtil.getIntegerValueHasDefaultFromRequest(request, "type", 1);
		Long mallId = HttpUtil.getLongValueFromRequest(request, "mallId");//店铺id
		String operateIdStr = request.getParameter("operateId");//操作成功的id，与type值做对应
		String mallName = request.getParameter("mallName");//页面用于传递，可以预防取次数据库
		Long price = HttpUtil.getLongValueFromRequest(request, "price");
		//end
		//biz start
		doSuccTuanCodeBiz(request, modelMap, type);
		//biz end
		Mall mall = null;
		
		if (StringUtils.isNotBlank(mallName)) {
			try {
				mallName = URLDecoder.decode(mallName, "UTF-8");
			} catch (UnsupportedEncodingException e) {
				mallName = "";
			}
		}
		if (StringUtils.isBlank(mallName)){
			mall = this.mallService.getMallById(mallId);
			mallName = mall.getMallName();
		}
		String mallItemJson = null;
		MallItemOrder mallItemOrder = null ;
		String tableNum = null;
		List<MallItemVO> mallItemVOList = null;
		if(StringUtils.isNotBlank(operateIdStr)){
			mallItemOrder = this.mallItemOrderService.getMallItemOrderById(Long.parseLong(operateIdStr));
			mallItemJson = mallItemOrder.getBuyItemsJson();//智慧餐厅下单成功给后厨
			if (mallItemJson != null) {
				mallItemVOList = JSONArray.parseArray(mallItemJson, MallItemVO.class);
			}
			if(mallItemOrder.getTableNumId() != null && mallItemOrder.getTableNumId() >0l){
				tableNum = this.tableNumService.getTableNumById(mallItemOrder.getTableNumId()).getNum();//桌号	
			}
		}
		
		User user = WebUtil.getCurrentUser(request);
		modelMap.put("mallItemOrder", mallItemOrder);
		modelMap.put("mallItemVOList", mallItemVOList);
		modelMap.put("operateIdStr", operateIdStr);
		modelMap.put("type", type);
		modelMap.put("mallName", mallName);
		modelMap.put("mall", mall);
		modelMap.put("user", user);
		modelMap.put("price", price);
		modelMap.put("mallId", mallId);
		modelMap.put("tableNum", tableNum);
		return "result/success";
		
	}
	@UserSecurity
	@RequestMapping("error")
	public String error(HttpServletRequest request,HttpServletResponse response,ModelMap modelMap){
		
		
		return "result/error";
		
	}
	
}
